<template>
  <div>
    <el-tree ref="tree" :data="list" :props="{label:'name'}" show-checkbox default-expand-all check-strictly node-key="id" />
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit()">确定</el-button>
      <el-button>取消</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/perimmions'
import { treeDataList } from '@/utils'
import { getRoleDetail, assignPermission } from '@/api/settings'
export default {
  name: 'VueHrAddPermissions',
  props: {
    id: {
      type: String,
      required: true
    }
  },

  data() {
    return {
      list: []
    }
  },

  mounted() {

  },
  created() {
    this.loadPermissionList()
    this.loadRoleDetail()
  },
  methods: {
    async hSubmit() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        const res = await assignPermission({ id: this.id, permIds: permIds })
        this.$message.success(res.message)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    async loadRoleDetail() {
      try {
        const res = await getRoleDetail(this.id)
        console.log(res)
        this.$refs.tree.setCheckedKeys(res.data.permIds)
      } catch (error) {
        console.log(error)
      }
    },
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log(res)
        this.$message.success(res.message)
        this.list = treeDataList(res.data)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
